package cn.com.dcsgo.api;

import cn.com.dcsgo.model.product.Category;
import cn.com.dcsgo.model.product.SkuInfo;
import cn.com.dcsgo.service.CategoryService;
import cn.com.dcsgo.service.SkuInfoService;
import cn.com.dcsgo.vo.product.SkuInfoVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * ProductApiController
 *
 * @author Dcsgo
 * @since 2025/4/17 上午11:31
 */
@RequiredArgsConstructor
@Api(tags = "商品RPC-API")
@RestController
@RequestMapping("api/product")
public class ProductApiController {
    private final CategoryService categoryService;
    private final SkuInfoService skuInfoService;

    @ApiOperation("通过skuId获取商品详细信息")
    @GetMapping("sku/vo/{skuId}")
    public SkuInfoVo getSkuInfoVo(@PathVariable Long skuId) {
        return skuInfoService.getSkuInfoVoById(skuId);
    }

    @ApiOperation("通过分类id获取分类信息")
    @GetMapping("category/{id}")
    public Category getCategoryById(@PathVariable Long id) {
        return categoryService.getById(id);
    }

    @ApiOperation("通过分类Ids获取分类信息")
    @GetMapping("category")
    public List<Category> getCategoryByIds(@RequestParam List<Long> ids) {
        return categoryService.listByIds(ids);
    }

    @ApiOperation("获取所有分类信息")
    @GetMapping("categories")
    public List<Category> getAllCategory() {
        return categoryService.list();
    }

    @ApiOperation("通过skuId获取sku信息")
    @GetMapping("sku/{id}")
    public SkuInfo getSkuInfoById(@PathVariable Long id) {
        return skuInfoService.getById(id);
    }

    @ApiOperation("通过skuIds获取sku信息")
    @PostMapping("sku")
    public List<SkuInfo> getSkuInfoList(@RequestBody List<Long> skuIds) {
        return skuInfoService.listByIds(skuIds);
    }

    @ApiOperation("获取指定数量的新人专享sku信息")
    @GetMapping("sku/new/user/count/{number}")
    public List<SkuInfo> getSkuInfoByCountForNewUser(@PathVariable Integer number) {
        //分页查询实现
        return skuInfoService.page(
                new Page<>(1, number),
                new LambdaQueryWrapper<SkuInfo>()
                        // 只查询上架的商品
                        .eq(SkuInfo::getPublishStatus, 1)
                        .eq(SkuInfo::getIsNewPerson, 1)
                        // 按 id 排序
                        .orderByDesc(SkuInfo::getId)
        ).getRecords();
    }

    @ApiOperation("根据skuName模糊查询sku信息")
    @GetMapping("sku/name/{keyword}")
    public List<SkuInfo> getSkuInfoByName(@PathVariable String keyword) {
        return skuInfoService.selectByName(keyword);
    }
}
